import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import { resolve } from 'path'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver(),
      IconsResolver({
        prefix: 'Icon',
      }),
      ],
    }),
    Components({
      resolvers: [ElementPlusResolver(),
      IconsResolver({
        enabledCollections: ['ep'],
      }),
      ],
    }),
    Icons({
      autoInstall: true,
    }),

  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  server: {
    host: true, // 指定服务器主机名
    port: 8880, // 指定服务器端口
    hmr: true,  // 开启热更新
    open: true, // 在服务器启动时自动在浏览器中打开应用程序
    https: false, // 是否开启 https
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:58004',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
        cookieDomainRewrite: {
          '*': '', // 将 Cookie 的域名重写为当前域名
        },
        followRedirects: true,
      },
    }
  },
  build: {
    rollupOptions: {
      input: {
        main: resolve(__dirname, 'index.html'),
        login: resolve(__dirname, './src/login/login.html'),
        findPwd: resolve(__dirname, './src/findPwd/findPwd.html'),
        mainInterface: resolve(__dirname, './src/mainInterface/mainInterface.html'),
      },
    },
  },
})

